/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_PLATFORM_H__
#define __EB_PLATFORM_H__


#include <kernel/device.h>
#include <kernel/driver.h>

extern struct bus_type platform_bus;


struct platform_device {
    struct device  device;
    const char  *compatible;
    void        *io_base;
    const void  *platform_data;
    const void  *private_data;
};


struct platform_driver {
    struct driver driver;
    int (*probe)(struct platform_device *dev);
    void (*remove)(struct platform_device *dev);
    struct match_id  *id_table;
};


#ifdef __cplusplus
extern "C" {
#endif

static inline struct platform_device* to_platform_device(struct device *dev)
{
    return container_of(dev, struct platform_device, device);
}


static inline struct platform_driver* to_platform_driver(struct driver *drv)
{
    return container_of(drv, struct platform_driver, driver);
}


static inline int platform_device_register(struct platform_device *pdev)
{
    pdev->device.bus = &platform_bus;
    return device_register(&pdev->device);
}


static inline void platform_device_unregister(struct platform_device *pdev)
{
    device_unregister(&pdev->device);
}


static inline int platform_driver_register(struct platform_driver *pdrv)
{
    pdrv->driver.bus = &platform_bus;
    return driver_register(&pdrv->driver);
}


static inline void platform_driver_unregister(struct platform_driver *pdrv)
{
    driver_unregister(&pdrv->driver);
}


static inline const void* platform_get_platform_data(struct platform_device *pdev)
{
    return pdev->platform_data;
}


static inline void platform_set_platform_data(struct platform_device *pdev, void *data)
{
    pdev->platform_data = data;
}


static inline const void* platform_get_private_data(struct platform_device *pdev)
{
    return pdev->private_data;
}


static inline void platform_set_private_data(struct platform_device *pdev, void *data)
{
    pdev->private_data = data;
}


#ifdef __cplusplus
}
#endif


#endif //!__EB_PLATFORM_H__